﻿using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SassModule.Contracts;
using SassModule.Entity;
using SassModule.ViewModel;
using System;
using System.Collections.Generic;

namespace SassModule.Controllers
{
    /// <summary>
    /// 角色
    /// </summary>
    [ApiController]
    [Route("Sass/[controller]")]
    public class RoleController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IWebHostEnvironment _env;
        private readonly IRoleService _service;
        private readonly IMapper _mapper;
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="env"></param>
        /// <param name="service"></param>
        public RoleController(IMapper mapper,ILogger<RoleController> logger, IWebHostEnvironment env, IRoleService service)
        {
            _logger = logger;
            _env = env;
            _service = service;
            _mapper = mapper;
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null,_service.Index(db)));
        }
        /// <summary>
        /// 根据Id获取角色信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        public IActionResult Index([FromRoute] Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            return Ok(new ResponseData(null, _service.Index(id, db)));
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add(RoleAddViewModel role)
        {
            string db = Common.GetHeader4DB(this.Request);
            var info = _mapper.Map<RoleAddViewModel, Role>(role);
            var res= _service.Add(info, db);
            if (res!=null)
            {
                return Ok(new ResponseData(res, null));
            }
            return Ok(new ResponseData(null,null));
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult Del(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Del(id, db);
            return Ok(new ResponseData(null, null));
        }
        /// <summary>
        /// 获取角色下的菜单
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpGet("{id}/menus")]
        public IActionResult GetMenus([FromRoute]Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            var menus = _service.GetMenus(id, db);
            return Ok(new ResponseData(null, menus));
        }
        /// <summary>
        /// 设置角色下的菜单
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <param name="menus">菜单列表</param>
        /// <returns></returns>
        [HttpPost("{id}/menus")]
        public IActionResult SetetMenus([FromRoute] Guid id,[FromBody] List<string> menus)
        {
            string db = Common.GetHeader4DB(this.Request);
             _service.SetMenus(id, menus, db);
            return Ok(new ResponseData(null, null));
        }

        /// <summary>
        /// 更改角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public IActionResult UpdateRole(Role role)
        {
            _service.UpdateRoles(role);
            return Ok(new ResponseData(null, null));
        }
    }
}
